#include "../include/proto/dispatcher.h"
#include "../include/proto/codec.h"
// #include "muduo/proto/query.pb.h"

#include "../include/muduo/base/Logging.h"
#include "../include/muduo/base/Mutex.h"
#include "../include/muduo/net/EventLoop.h"
#include "../include/muduo/net/TcpClient.h"

#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include "../include/muduo/net/TcpClient.h"
#include "../include/muduo/net/EventLoopThread.h"
#include "../include/muduo/net/TcpConnection.h"
#include "../include/muduo/base/CountDownLatch.h"
#include <functional>
#include <string>
#include "request.pb.h"

class Client
{
public:
    typedef std::shared_ptr<google::protobuf::Message> MessagePtr;
    typedef std::shared_ptr<Message_Queue::TranslateResponse> TranslateResponsePtr;
    typedef std::shared_ptr<Message_Queue::AddResponse> AddResponsePtr;
    Client(const std::string& sip,int sport)
        :_latch(1)
        ,_client(_loopthread.startLoop(),muduo::net::InetAddress(sip,sport),"Client")
        ,_dispatcher(std::bind(&Client::onUnknownMessage,this,std::placeholders::_1
        ,std::placeholders::_2,std::placeholders::_3))
        ,_codec(std::bind(&ProtobufDispatcher::onProtobufMessage,&_dispatcher
        ,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3))
    {
        // 注册业务请求处理函数。
        _dispatcher.registerMessageCallback<Message_Queue::TranslateResponse>(
        std::bind(&Client::onTranslate, this, std::placeholders::_1
            ,std::placeholders:: _2
            ,std::placeholders:: _3));
        _dispatcher.registerMessageCallback<Message_Queue::AddResponse>(
        std::bind(&Client::onAdd, this, std::placeholders::_1
            ,std::placeholders:: _2
            ,std::placeholders:: _3));
        _client.setConnectionCallback(
        std::bind(&Client::onConnection, this, std::placeholders::_1));
        _client.setMessageCallback(
        std::bind(&ProtobufCodec::onMessage, &_codec, std::placeholders::_1
            ,std::placeholders:: _2
            ,std::placeholders:: _3));;
    }
    void Translate(const std::string& msg)
    {
        Message_Queue::TranslateRequest req;
        req.set_msg(msg);
        send(&req);
    }
    void Add(int num1,int num2)
    {
        Message_Queue::AddRequest req;
        req.set_num1(num1);
        req.set_num1(num2);
        send(&req);
    }
    void connect()
    {
        _client.connect();
        // 阻塞直到成功链接为止。
        _latch.wait();
    }
private:
    // 子类赋值给父类，形成多态。
    void send(const google::protobuf::Message* message)
    {
        _codec.send(_conn,*message);
    }
    void onTranslate(const muduo::net::TcpConnectionPtr conn
        ,const TranslateResponsePtr& message
        ,muduo::Timestamp)
    {
        std::cout << "翻译结果：" << message->msg() <<std::endl;
    }
        void onConnection(const muduo::net::TcpConnectionPtr& conn)
    {
        if(conn->connected() == true)
        {
            LOG_INFO << "新链接已建立" ;
        }
        else
        {
            LOG_INFO << "链接即将关闭" ;
        }
    }
    void onAdd(const muduo::net::TcpConnectionPtr& conn
        ,const AddResponsePtr& message
        ,muduo::Timestamp)
    {
        std::cout << "相加结果：" << message->result() <<std::endl;
    }
    void onUnknownMessage(const muduo::net::TcpConnectionPtr& conn
        ,const MessagePtr& message,muduo::Timestamp)
    {
        LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
        conn->shutdown();
    }
private:
    muduo::CountDownLatch _latch;  // 实现同步
    muduo::net::EventLoopThread _loopthread;  // 异步循环处理线程
    muduo::net::TcpClient _client;  // 客户端
    muduo::net::TcpConnectionPtr _conn; // 客户端对应的连接
    // muduo::net::EventLoop _baseloop;  // 执行
    ProtobufDispatcher _dispatcher; // 请求分发器
    ProtobufCodec _codec; // 协议处理器
};

int main()
{
    Client client("127.0.0.1",8085);
    client.connect();
    client.Translate("Hello");
    client.Add(10,20);
    sleep(1);


    return 0;
}